package com.zebra.demo.person.rest;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zebra.base.auto.SimpleQueryWrapperBuilder;
import com.zebra.base.po.BaseEntity;
import com.zebra.base.rest.BaseController;
import com.zebra.base.vo.Res;
import com.zebra.base.vo.VuePageRes;
import com.zebra.demo.person.entity.DemoPerson;
import com.zebra.demo.person.service.DemoPersonService;
import com.zebra.demo.person.view.DemoPersonForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 销魂钉
 * @since 2020-05-28
 */
@RestController
@RequestMapping("/demo/person/")
public class DemoPersonController extends BaseController {

    @Autowired
    private DemoPersonService demoPersonService;
    /**
     * 测试基本查询功能
     * @Description: TODO
     * @auther: litaolin
     * @date: 12:37:30 2020-05-29
     * @param: []
     * @return: java.util.List<com.zebra.demo.person.entity.DemoPerson>
     *
     */
    @GetMapping("/list")
    public List<DemoPerson> queryList() {
        System.out.println(DateUtil.now());
        List<DemoPerson> list = demoPersonService.list();
        return list;
    }

    /**
     * 测试分页
     * @Description: TODO
     * @auther: litaolin
     * @date: 12:36:30 2020-05-29
     * @param: []
     * @return: java.util.List<com.zebra.demo.person.entity.DemoPerson>
     *
     */
    @GetMapping("/queryPageList")
    public Res<BaseEntity> queryPageList(@RequestBody DemoPersonForm forms) {
        System.out.println(DateUtil.now());
        QueryWrapper<DemoPerson> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(forms.getName()), "name", forms.getName());
        IPage<BaseEntity> demoPersonIPage = demoPersonService.query(forms.getPage(),queryWrapper);
        return Res.success(new VuePageRes<>().builder(demoPersonIPage));
    }


    /**
     * 测试分页
     * @Description: TODO
     * @auther: litaolin
     * @date: 12:36:30 2020-05-29
     * @param: []
     * @return: java.util.List<com.zebra.demo.person.entity.DemoPerson>
     *
     */
    @GetMapping("/queryAutoPageList")
    public Res<BaseEntity> queryAutoPageList(@RequestBody DemoPersonForm forms) {
        System.out.println(DateUtil.now());
//        QueryWrapper<DemoPerson> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(StringUtils.isNotBlank(forms.getName()), "name", forms.getName());
        QueryWrapper builder = SimpleQueryWrapperBuilder.getInstance().builder(forms);
        IPage<BaseEntity> demoPersonIPage = demoPersonService.query(forms.getPage(),builder);
        return Res.success(new VuePageRes<>().builder(demoPersonIPage));
    }

    /**
     * 自动保存
     * @param forms
     * @return
     */
//    @GetMapping("/autoSave")
//    public Res<BaseEntity> queryAutoSave(@RequestBody DemoPersonForm forms) {
////        System.out.println(DateUtil.now());
////        QueryWrapper<DemoPerson> queryWrapper = new QueryWrapper<>();
////        queryWrapper.like(StringUtils.isNotBlank(forms.getName()), "name", forms.getName());
//
////        IPage<BaseEntity> demoPersonIPage = demoPersonService.query(forms.getPage(),SimpleQueryWrapperBuilder.getInstance().builder(forms));
////        return Res.success(new VuePageRes<>().builder(demoPersonIPage));
//        boolean b = demoPersonService.save(forms.toPo(DemoPerson.class));
//        return new Res<>();
//    }


}

